package com.test.listener;

import jakarta.annotation.Resource;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import java.util.Map;
@Component
@RabbitListener(queues = "emailQueue")
public class EmailListener {
    @Resource
    JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    String username;
    @RabbitHandler
    public void process(Map<String,Object> data){
        System.out.println("进入消费者");
        String type = (String) data.get("type");
        String email = (String) data.get("email");
        int code = (int) data.get("code");
        SimpleMailMessage message = switch (type)  {
            case "register" -> createMessage("这是注册信息，有效时间为3分钟，请妥善保管","你的验证码为:"+code,email);
            case "reset" -> createMessage("这是重置密码信息，有效时间为3分钟，请妥善保管","你的验证码为:"+code,email);
            case "Modify" -> createMessage("这是修改邮箱信息，有效时间为3分钟，请妥善保管","你的验证码为:"+code,email);
            default -> null;
        };
        if(message == null) return;
        javaMailSender.send(message);



    }
    private SimpleMailMessage createMessage(String title,String text,String email){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject(title);
        message.setFrom(username);
        message.setTo(email);
        message.setText(text);
        return message;
    }
}
